import {
  RecommendProductAction,
  FETCH_RECOMMEND_PRODUCTS_START,
  FETCH_RECOMMEND_PRODUCTS_SUCCESS,
  FETCH_RECOMMEND_PRODUCTS_FAIL,
} from "./recommend-products-actions"
// recommendProducts的State
interface RecommendProductsState {
  productList: {
    productList1: any[]
    productList2: any[]
    productList3: any[]
  }
  loading: boolean
  error: string | null
}

const defaultState: RecommendProductsState = {
  loading: true,
  error: null,
  productList: {
    productList1: [],
    productList2: [],
    productList3: [],
  },
}

 const recommendReducer=(state = defaultState, action: RecommendProductAction) => {
  switch (action.type) {
    // 一开始请求的时候，只需加载loading
    case FETCH_RECOMMEND_PRODUCTS_START:
      return {
        ...state,
        loading: true,
      }
    // 请求成功，关闭loading，更新数据
    case FETCH_RECOMMEND_PRODUCTS_SUCCESS:
      return {
        ...state,
        loading: false,
        productList: action.payload,
      }
    // 请求失败，关闭loading，更新错误信息
    case FETCH_RECOMMEND_PRODUCTS_FAIL:
      return {
        ...state,
        loading: false,
        error: action.payload,
      }
    default:
      return state
  }
}

export default recommendReducer
